import {MarkdownView, TFile} from "obsidian";
import MyPlugin from "../../../main";
import {
	recentOpenFileMdPath,
} from "../全局变量";

// 添加页面的操作
export function 打开常用文件命令(this_:MyPlugin){
    // 打开最近的文件
	this_.addCommand({
		id: 'open-file-recent',
		name: 'sjz最近打开的文件',
		callback: () => openSpecificFile(	recentOpenFileMdPath, this_),
	});

    this_.addCommand({
        id: 'open-file-one',
        name: 'sjz打开指定文件1',
        callback: () => openSpecificFile("1",this_),
    });

    // 添加打开指定文件的命令
    this_.addCommand({
        id: 'open-file-two',
        name: 'sjz打开指定文件2',
        callback: () => openSpecificFile("2",this_),
    });

    // 添加打开指定文件的命令
    this_.addCommand({
        id: 'open-file-three',
        name: 'sjz打开指定文件3',
        callback: () => openSpecificFile("3",this_),
    });

    // 添加打开指定文件的命令
    this_.addCommand({
        id: 'open-file-four',
        name: 'sjz打开指定文件4',
        callback: () => openSpecificFile("4",this_),
    });

    // 添加打开指定文件的命令
    this_.addCommand({
        id: 'open-file-five',
        name: 'sjz打开指定文件5',
        callback: () => openSpecificFile("5",this_),
    });

    // 添加打开指定文件的命令
    this_.addCommand({
        id: 'open-file-fix',
        name: 'sjz打开指定文件6',
        callback: () => openSpecificFile("6",this_),
    });
}

function openSpecificFile(number: string,this_:MyPlugin) {
	console.log("打开指定文件"+number);
    let fileToOpenPath = this_.settings.filePath1;
    let fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath1);
    if(number==="1"){
        fileToOpenPath = this_.settings.filePath1;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath1);
    }else if(number==="2"){
        fileToOpenPath = this_.settings.filePath2;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath2);
    }else if(number==="3"){
        fileToOpenPath = this_.settings.filePath3;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath3);
    }else if(number==="4"){
        fileToOpenPath = this_.settings.filePath4;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath4);
    }else if(number==="5"){
        fileToOpenPath = this_.settings.filePath5;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath5);
    }else if(number==="6"){
        fileToOpenPath = this_.settings.filePath6;
        fileToOpen = this_.app.vault.getAbstractFileByPath(this_.settings.filePath6);
    }else {
        fileToOpenPath = number;
        fileToOpen = this_.app.vault.getAbstractFileByPath(number);
    }
    // if (fileToOpen instanceof TFile) {
    //     this_.app.workspace.openLinkText(fileToOpen.path, '/', true);
    // } else {
    //     console.error('File not found');
    // }

    if (fileToOpen instanceof TFile) {
        let foundLeaf = null;
        // 检查文件是否已经打开
        this_.app.workspace.getLeavesOfType('markdown').forEach(leaf => {
            const markdownView = leaf.view as MarkdownView; // 这里进行类型断言
			console.log("检查文件是否已经打开"+fileToOpenPath);
            if (markdownView.file && markdownView.file.path === fileToOpenPath) {
                foundLeaf = leaf;
            }
        });

        if (foundLeaf) {
            // 如果文件已经打开，激活对应的标签
            this_.app.workspace.setActiveLeaf(foundLeaf, true, true);
        } else {
            this_.app.workspace.openLinkText(fileToOpen.path, '/', false);
        }
    } else {
        console.error('File not found');
    }
}
